<template>

  <div class="all">
    <h1>开放实验室使用申请</h1>
    <el-card shadow="always" body-style="padding:50px">
    <div class="container">

      <div class="row1">
         <el-form :inline="true" :model="row1" :rules="row1Rules" ref="refreshRefes">
               <el-form-item label="使用原因：" prop="reason_use">
                  <el-input v-model="row1.reason_use"></el-input>
               </el-form-item>

               <el-form-item label="项目名称：" prop="porject_name">
                   <el-input v-model="row1.porject_name"></el-input>
              </el-form-item>

              <el-form-item label="申请人：" prop="applicant">
          <el-input v-model="row1.applicant" @input="change($event)" disabled></el-input>
        </el-form-item>

        </el-form>
    </div>

    <div class="mainForm"> 
      <el-form :inline="true" :model="mainForm" :rules="mainFormRules" ref="refreshRefss">
        <!-- <el-form-item label="表单编号：" prop="form_id">
                  <el-input v-model="mainForm.form_id"></el-input>
               </el-form-item> -->

               <el-form-item label="申请人姓名:" prop="applicant_name">
                  <el-input v-model="mainForm.applicant_name"></el-input>
               </el-form-item>

               <el-form-item label="表单种类:" prop="type_id">
                  <el-input v-model="mainForm.type_id" disabled></el-input>
               </el-form-item>
      </el-form>
    </div>
    <div class="row2">
      <el-form :inline="true" :model="row2" :rules="row2Rules" ref="refreshRefs">
        <el-form-item label="使用时间：" prop="start_time">
          <el-input v-model="row2.start_time" placeholder="比如：2021-10-24"></el-input>
        </el-form-item>
        <el-form-item label="结束时间：" prop="end_time">
          <el-input v-model="row2.end_time" placeholder="比如：2021-10-24"></el-input>
        </el-form-item>
     <!-- <el-form-item label="使用时间："  prop="year1">
          <el-row>
            <el-col>
               <el-input v-model="row2.year1" style="width:64px"  maxlength="4"></el-input>
          </el-col>
        </el-row>
        </el-form-item>
        
        <el-form-item label="年" style="margin-left:24px"></el-form-item>

      <el-form-item prop="month1">
          <el-row :gutter="10">
            <el-col :span="1">
          <el-input v-model="row2.month1" style="width:60px" maxlength="2"></el-input>
          </el-col>
        </el-row>
        </el-form-item>

        <el-form-item label="月" style="margin-left:25px"></el-form-item>

        <el-form-item prop="day1">
          <el-row :gutter="10">
            <el-col :span="1">
          <el-input v-model="row2.day1" style="width:60px" maxlength="2"></el-input>
          </el-col>
        </el-row>
        </el-form-item>

        <el-form-item label="日" style="margin-left:25px"></el-form-item>

          <el-form-item label="至"></el-form-item>
       
    <el-form-item prop="year2">
          <el-row>
            <el-col>
               <el-input v-model="row2.year2" style="width:64px" maxlength="4"></el-input>
          </el-col>
        </el-row>
        </el-form-item>
        
        <el-form-item label="年" style="margin-left:24px"></el-form-item>

      <el-form-item prop="month2">
          <el-row :gutter="10">
            <el-col :span="1">
          <el-input v-model="row2.month2" style="width:60px" maxlength="2"></el-input>
          </el-col>
        </el-row>
        </el-form-item>

        <el-form-item label="月" style="margin-left:25px"></el-form-item>

        <el-form-item prop="day2">
          <el-row :gutter="10">
            <el-col :span="1">
          <el-input v-model="row2.day2" style="width:60px" maxlength="2"></el-input>
          </el-col>
        </el-row>
        </el-form-item>

        <el-form-item label="日" style="margin-left:25px"></el-form-item> -->

     <el-form-item label="每天8:00-22:00"></el-form-item>
    <el-button type="primary" @click="addStudent()">添加学生</el-button>
     </el-form>
    </div>

    <div class="row3">
      <span>申请学生名单：</span>
      <el-table :data="userList" border stripe>
  <el-table-column label="表单编号" prop="list_id"></el-table-column>
  <el-table-column label="学生姓名" prop="student_name"></el-table-column>
  <el-table-column label="学号" prop="student_id"></el-table-column>
  <el-table-column label="联系电话" prop="phone"></el-table-column>
  <el-table-column label="承担工作（角色）" prop="work"></el-table-column>
  <el-table-column label="操作" width="180px">
    <template slot-scope="scope">
      <!-- 修改按钮 -->
      <el-button type="primary" size="mini" @click="changeUser(scope.row)">修改</el-button>
      <!-- 删除按钮 -->
      <el-button type="danger" size="mini" @click="removeUser(scope.row)">删除</el-button>
    </template>
  </el-table-column>
</el-table>
    </div>

    </div>

    <div class="footer">
      <div class="f2">
      <el-button type="primary" size="mini" @click="refresh();open()">提交申请</el-button>
      </div>
      <div class="f3">
        <hr>
        疫情特殊时期，我承诺学生进入实验室后第一时间给学生宣讲《实验室疫情个人防护常识》,
          并拍照存档。在实验室期间要求同学们隔位而坐。配合实验中心相关工作安排。
      </div>
    </div>
  <!-- 添加用户的对话框 -->
        <el-dialog
         title="添加学生" 
         :visible.sync="addDialogVisible"
         width="50%"
         @close="addDialogClosed"
           >
          <el-form :model="addForm" ref="addFormRef" label-width="80px"  :rules="addFormRules">
             <el-form-item label="表单编号" prop="form_id">
              <el-input v-model="addForm.form_id" disabled></el-input>
            </el-form-item> 
            <el-form-item label="学生姓名" prop="student_name">
              <el-input v-model="addForm.student_name"></el-input>
            </el-form-item>
            <el-form-item label="学号" prop="student_id">
              <el-input v-model="addForm.student_id"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="addForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="承担工作" prop="work">
              <el-input v-model="addForm.work"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
             <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addUser()">确定</el-button>
            <!-- @click="addDialogVisible=false" -->
          </span>
        </el-dialog>

        <!-- 修改用户的对话框 -->
        <el-dialog title="修改学生信息" :visible.sync="changeDialogVisible" width="50%" @close="changeDialogClosed">
          <el-form :model="changeForm" ref="changeFormRef" label-width="80px" :rules="changeFormRules">
             <el-form-item label="表单编号" prop="list_id">
              <el-input v-model="changeForm.list_id" disabled></el-input>
            </el-form-item>
            <el-form-item label="学生姓名" prop="student_name">
              <el-input v-model="changeForm.student_name" disabled></el-input>
            </el-form-item>
            <el-form-item label="学号" prop="student_id">
              <el-input v-model="changeForm.student_id" ref="changid"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="phone"  ref="changphone">
              <el-input v-model="changeForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="承担工作" prop="work"  ref="changword">
              <el-input v-model="changeForm.work"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="sureChangeUser()">确定</el-button>
          </span>
        </el-dialog>
        </el-card>
  </div>

</template>

<script>
export default {
  data() {
      var accounts = sessionStorage.getItem('accounts');
      var checkPhone = (rule, value, cb) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        // 合法手机号
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    },
     // 验证时间的规则
     checkTime = (rule, value, cb) => {
      // 验证时间的正则表达式
      const regTime = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
      if (regTime.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error("请输入合法的时间！正确格式为：2014-01-01"))
    }
    return {
    //  flag:true,
            // 控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      // 控制修改用户对话框的显示与隐藏
      changeDialogVisible: false,
      
      addForm: {
        form_id: "",  
        student_name: "",
        student_id: "",
        phone:"",
        work: ""
      },
      changeForm: {
        list_id: "",  
        student_name: "",
        student_id: "",
        phone:"",
        work: ""
      },
      userList: [],
      row1:
       {
          reason_use:"",
          porject_name:"",
          applicant:accounts
       },
      row2:
        {
          start_time: "",
          end_time: ""
        },
      row3:
        {
         list_id:"",
         student_name:"",
          student_id:"",
          phone:"",
          work: ""
        },
        mainForm:
        {
          form_id: "",
          applicant_name: "",
          type_id: "5"
        },
      addFormRules: {
        list_id: [
          { required: true, message: '请输入表单编号', trigger: 'blur' }
        ],
        student_name: [
          { required: true, message: '请输入学生姓名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        student_id: [
          { required: true, message: '请输入学号', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        work: [
          { required: true, message: '请输入承担的工作', trigger: 'blur' }
        ]
      },
      changeFormRules: {
         list_id: [
          { required: true, message: '请输入学表单编号', trigger: 'blur' }
        ],
        student_name: [
          { required: true, message: '请输入学生姓名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        student_id: [
          { required: true, message: '请输入学号', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        work: [
          { required: true, message: '请输入承担的工作', trigger: 'blur' }
        ]
      },
      row1Rules: {
        reason_use: [
           { required: true, message: '请输入使用原因', trigger: 'blur' }
        ],
        porject_name: [
           { required: true, message: '请输入项目名称', trigger: 'blur' }
        ]
      },
      mainFormRules: {
         form_id: [
           { required: true, message: '请输入表单编号', trigger: 'blur' }
        ],
        applicant_name: [
           { required: true, message: '请输入申请人姓名', trigger: 'blur' }
        ]
      },
      row2Rules: {
        start_time: [
           { required: true, message: '请输入开始时间', trigger: 'blur' },
           { validator: checkTime, trigger: 'blur' }
        ],
        end_time: [
           { required: true, message: '请输入停止时间', trigger: 'blur' },
           { validator: checkTime, trigger: 'blur' }
        ]
        // year1: [
        //    { required: true, message: '请输入年份', trigger: 'blur' }
        // ],
        // month1: [
        //   { required: true, message: '请输入月份', trigger: 'blur' }
        // ],
        // day1: [
        //   { required: true, message: '请输入日期', trigger: 'blur' }
        // ],
        // year2: [
        //    { required: true, message: '请输入年份', trigger: 'blur' }
        // ],
        // month2: [
        //   { required: true, message: '请输入月份', trigger: 'blur' }
        // ],
        // day2: [
        //   { required: true, message: '请输入日期', trigger: 'blur' }
        // ]
      },
      footerFormRules: {
         applicant: [
           { required: true, message: '请输入申请人姓名', trigger: 'blur' }
        ],
        year3: [
           { required: true, message: '请输入年份', trigger: 'blur' }
        ],
        mon3: [
          { required: true, message: '请输入月份', trigger: 'blur' }
        ],
        day3: [
          { required: true, message: '请输入日期', trigger: 'blur' }
        ],
      }
    }
  },
  created(){
    this.flag();
  },
  methods:{
   
    flag(){
      sessionStorage.setItem('flag',1)
    },
    async addStudent() {
      var flag= sessionStorage.getItem('flag')
      // console.log(this.row1);
      if (this.row1.reason_use !== ""&&this.row1.porject_name !== ""&&this.row2.start_time !== ""&&this.row2.end_time !== ""&&this.mainForm.applicant_name !== ""&&flag==1){
    //  console.log(123);
     const{data: res} = await this.$http.post('/api/student/openlabborrow',{reason_use:this.row1.reason_use,porject_name:this.row1.porject_name,start_time:this.row2.start_time,end_time:this.row2.end_time})
          // console.log(res);
          // console.log(res.data.form_id);
          var idid=res.data.form_id
          sessionStorage.setItem('idid',idid);
          if (res.code == 200){
            this.$message.success('提交成功！')
            const{data: res} = await this.$http.post('/api/student/createform',{form_id:idid,applicant_name:this.mainForm.applicant_name,type_id:this.mainForm.type_id})
            console.log(123);
            console.log(res);
            if (res.code == 200){
              this.$message.success('表单创建成功!')
              this.addDialogVisible = true
              this.addForm.form_id = sessionStorage.getItem('idid')
            }
            if(res.code == 100){
              this.$message.error("表单创建失败！")
            }
          }
          if (res.code == 100 ){
            this.$message.error('提交失败！')
          }
          return sessionStorage.setItem('flag',0)
      }
      else if (this.row1.reason_use !== ""&&this.row1.porject_name !== ""&&this.row2.start_time !== ""&&this.row2.end_time !== ""&&this.mainForm.applicant_name !== ""&&flag==0){
        
        var idid=sessionStorage.getItem('idid')
        console.log(idid)
        //  const{data: res} = await this.$http.post('/api/student/createform',{form_id:idid,applicant_name:this.mainForm.applicant_name,type_id:this.mainForm.type_id})
        //     console.log(456);
        //     console.log(res);
        //     if (res.code == 200){
        //       this.$message.success('表单创建成功!')
        //       this.addDialogVisible = true
        //       this.addForm.form_id = sessionStorage.getItem('idid')
        //     }
        //     if(res.code == 100){
        //       this.$message.error("表单创建失败！")
        //     }
        //  this.$refs.addFormRef.validate(async (valid) => {
        // eslint-disable-next-line no-useless-return
        // if (!valid) return;
      //  const{data: res} = await this.$http.post('/api/student/openlabaddstu',this.addForm);
      // console.log(idid);
      // console.log(this.mainForm.applicant_name);
      // console.log(this.mainForm.type_id);
      this.addDialogVisible = true
      // const{data: res} = await this.$http.post('/api/student/createform',{form_id:idid,applicant_name:this.mainForm.applicant_name,type_id:this.mainForm.type_id})
      // console.log(this.addForm);
      // const{data: res} = await this.$http.post('/api/student/openlabaddstu',this.addForm);
      //  console.log(res);
      //  if(res.code !== 200) return this.$message.error('添加用户失败！')
        // this.addDialogVisible = true
        // // 可以发起添加的网络请求
        // this.$message.success('添加用户成功！')
        // this.userList.push(res.data)
        // 隐藏添加用户的对话框
        // this.addDialogVisible=false
        // 重新获取用户列表数据
        // this.getUserList()
      // })
      }
    },
    open() {
        this.$message({
          message: '提交成功',
          type: 'success',
          center: true
        });
      },
      // open1() {
      //   this.$message({
      //     message: '添加成功',
      //     type: 'success',
      //     center: true
      //   });
      // },
      // open2() {
      //   this.$message({
      //     message: '修改成功',
      //     type: 'success',
      //     center: true
      //   });
      // },
  change() {
	    this.$forceUpdate()
    },  
     addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
  changeDialogClosed() {
      this.$refs.changeFormRef.resetFields()
    },
        // 点击按钮，添加用户
    async addUser() {
      
      console.log(this.addForm.form_id);
      // console.log(123);
       this.addDialogVisible=true
      this.$refs.addFormRef.validate(async (valid) => {
        // eslint-disable-next-line no-useless-return
        if (!valid) return;
       const{data: res} = await this.$http.post('/api/student/openlabaddstu',this.addForm);
       console.log(res);
       if(res.code !== 200) return this.$message.error('添加用户失败！')
        // 可以发起添加的网络请求
        this.$message.success('添加用户成功！')
        this.userList.push(res.data)
        // 隐藏添加用户的对话框
        this.addDialogVisible=false
        // 重新获取用户列表数据
        // this.getUserList()
        console.log(this.userList);
      })
    },
    //修改弹出框获取userlist数据
    async  changeUser(info) {
      // var val = info.form_id
      // console.log(val);
      this.changeDialogVisible= true
      // console.log(info.form_id);
      // this.changeForm=info
      // console.log(info);
     
      // console.log('------------');
       const {data:res} = await this.$http.post('/api/student/openlabselectstuone',{list_id:info.list_id})
      // console.log(123);
      // console.log(res); 
      // console.log(res.data[0]);
      this.changeForm=res.data[0]
      // console.log(this.changeForm);
    },
    sureChangeUser() {
         var idid=sessionStorage.getItem('idid')
         console.log(idid);
      this.$refs.changeFormRef.validate(async(valid) => {
        if(!valid) return;
        const {data: res} = await this.$http.post('/api/student/openlabupdatestu',this.changeForm);
        console.log(res);
        if(res.code !== 200) return this.$message.error('修改用户失败！')
        this.$message.success('修改用户成功！')
        const{data: resss} = await this.$http.post('/api/student/openlabselectstu',{form_id:idid})
        console.log(resss);
        // this.userList.push(this.changeForm)
        // console.log();
        // this.userList.push(res.data)
        this.userList=resss.data
      //  this.changeForm = resss.data[0]
        this.changeDialogVisible=false
      // this.userList=this.changeForm
       
      // console.log(this.userList);

          // this.userList.student_id = this.changeForm.student_id
          // this.userList.phone = this.changeForm.phone
          // this.userList.work = this.changeForm.work
       

        // console.log(this.userList[0].form_id);
        // console.log( this.userList.student_id);
        // console.log(  this.userList.phone);
        // console.log(this.userList.work);
        // this.addUser()
        
      

        // const{data: ress} = await this.$http.post('/api/student/openlabselectstu',{form_id:idid})
        // console.log(ress);
        // this.getUserList()
        // console.log(res.data);
        // this.changeForm=res.data
        // console.log(this.chan);
        // console.log(this.userList);
      })
    },
    //删除
        async removeUser(account) {
          var idid=sessionStorage.getItem('idid')
          console.log(account);
      const ConfirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (ConfirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const {data: res} = await this.$http.post('/api/student/openlabdeletestu',
      {list_id: account.list_id}
     )
     console.log(res);
      if (res.code == 200){
        this.$message.success('删除用户成功！')
        const {data: res1} = await this.$http.post('/api/student/openlabselectstu',{form_id:idid})
      console.log(res1);
      this.userList=res1.data
      }
      if (res.data == 100){
        this.$message.error('删除用户失败！')
      }
      // const { data: res } = await this.$http.delete("users/" + id);
      // if (res.meta.status !== 200) {
      //   return this.$message.error("删除用户失败！");
      // }
      // this.$message.success("删除用户成功！");
      // this.userList();
    },
    refresh() {
      // this.$refs.refreshRef.resetFields()
      this.userList=[]
      this.$refs.refreshRefs.resetFields()
      this.$refs.refreshRefes.resetFields()
      this.$refs.refreshRefss.resetFields()
    }
  }
}
</script>

<style>
h1 {
  text-align: center;
}
.el-input {
    position: relative;
    font-size: 14px;
    display: inline-block;
}
.el-row {
    box-sizing: border-box;
    width: 40px;
}
.footer {
  margin-top: 15px;
}
.f2 {
  text-align: center;
  margin: 19px;
}
.f3 {
  color:grey;
}
.row3 span {
  margin-top: 15px;
}

</style>